home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / lib / timeval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  1.4 KB  |  72 lines

  1. /*
  2.      net logger - network traffic logging library
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. timeval.c - 03/20/93
  8.  
  9. */
  10. #include <sys/time.h>
  11.  
  12. #define MICPSEC 1000000
  13.  
  14. float
  15. microsec(struct timeval tv)
  16. {
  17.      return (float)tv.tv_sec*MICPSEC+tv.tv_usec;
  18. }
  19.  
  20. struct timeval
  21. subtime(struct timeval t1, struct timeval t2)
  22. {
  23.      struct timeval result;
  24.  
  25.      result.tv_sec = t1.tv_sec - t2.tv_sec;
  26.  
  27.      if(t1.tv_usec < t2.tv_usec){
  28.       result.tv_sec--;
  29.       result.tv_usec = MICPSEC-t2.tv_usec + t1.tv_usec;
  30.      }
  31.      else
  32.       result.tv_usec = t1.tv_usec - t2.tv_usec;
  33.      
  34.      return result;
  35. }
  36.  
  37. struct timeval
  38. addtime(struct timeval t1, struct timeval t2)
  39. {
  40.      struct timeval result;
  41.      
  42.      result.tv_sec = t1.tv_sec + t2.tv_sec;
  43.  
  44.      if((result.tv_usec = t1.tv_usec + t2.tv_usec) > MICPSEC){
  45.       result.tv_usec -= MICPSEC;
  46.       result.tv_sec++;
  47.      }
  48.  
  49.      return result;
  50. }
  51.  
  52. char *
  53. gettimestr(struct timeval tp)
  54. {
  55.      struct tm *tm;
  56.      static char buf[50];
  57.      static char buf2[8];
  58.  
  59.      tm = localtime(&tp.tv_sec);
  60.      strftime(buf, 25, "%D %H:%M:%S", tm);
  61.      sprintf(buf2, ".%02lu", (unsigned long)(tp.tv_usec/(MICPSEC/100)));
  62.      strcat(buf, buf2);
  63.      return buf;
  64. }
  65. void
  66. outtime(struct timeval tp)
  67. {
  68.      char *buf;
  69.      buf = gettimestr(tp);
  70.      printf("%s", buf);
  71. }
  72.